from unittest import result
from sqlalchemy import insert, select, update
from app.api.admin.model.shop.shop import ShopModel
from app.api.admin.schema.shop.shop import ShopInSchema, ShopOutSchema
from app.dependencies.database import db


class ShopService():
    async def detail(self):
        query = select(ShopModel).select_from(ShopModel)
        return await db.fetch_one(query)

    async def edit(self, shop_in: ShopInSchema):
        result = 0
        # 先查询有数据没
        shop = await self.detail()
        print(shop)
        # 如果有更新
        if shop:
            result = await db.execute(update(ShopModel).where(ShopModel.id == ShopOutSchema.model_validate(shop).id).values(**shop_in.model_dump()))
            return result
        # 如果没有新增
        result = await db.execute(insert(ShopModel).values(**shop_in.model_dump()))
        return result

    async def update_image(self, shop_in: ShopInSchema):
        return ""

    @classmethod
    async def instance(cls):
        return cls()
